package com.tek.ding.faceding.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.tek.ding.faceding.R;
import com.tek.ding.faceding.utils.BaseTools;
import com.tek.ding.faceding.utils.MyToastUtils;


public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView {

    protected static final String INTENT_DATA = "intent_data";

    private ProgressDialog dialog;

    protected P presenter;

    protected Toolbar toolBar;
    protected ImageView tbBack;
    protected TextView tbTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutId());

        presenter = createPresenter();

        setToolBar();
        tbTitle = find(R.id.tbTitle);
        tbTitle.setText(!TextUtils.isEmpty(setTitleName()) ? setTitleName() : "");

        init();
    }

    public static void startActivity(Context context, Class c) {
        context.startActivity(new Intent(context, c));
    }

    protected abstract int getLayoutId();

    protected abstract String setTitleName();

    protected abstract P createPresenter();

    protected abstract void init();

    protected <T extends View> T find(@IdRes int id) {
        return findViewById(id);
    }

    /**
     * 设置titlebar
     */
    protected void setToolBar() {
        toolBar = find(R.id.toolBar);
        tbBack = find(R.id.tbBack);
        setSupportActionBar(toolBar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //去除默认Title显示
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(false);
//            actionBar.setHomeAsUpIndicator(R.mipmap.ic_back);
        }

        tbBack.setOnClickListener(v -> {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                finishAfterTransition();
            } else {
                onBackPressed();
            }
        });
    }

    /**
     * toolBar隐藏返回按钮
     */
    public void setTitleBackGone() {
        tbBack.setVisibility(View.GONE);
    }

    @Override
    public void showLoading() {
        dialog = new ProgressDialog(this);
        dialog.setMessage("loading......");
        dialog.show();
    }

    @Override
    public void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void showError(String msg) {
        MyToastUtils.showLong(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseTools.hideSoftKeyBoard(this);
    }
}
